package com.vrv.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;

import com.vrv.entity.AdminInfo;
import com.vrv.entity.DbInfo;
import com.vrv.entity.MenuInfo;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.XMLRead;

@Service
public class MenuService {

    public List<MenuInfo> showMenuList(HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<MenuInfo> rootTrees = new ArrayList<MenuInfo>();
        if (session.getAttribute("type") != null) {
            String menuIds = null;
            if (Integer.parseInt(session.getAttribute("type").toString()) == 1) {
                AdminInfo userInfo = (AdminInfo) session.getAttribute("userInfo");
                menuIds = userInfo.getPerms();
            } else {
                // UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
                menuIds = "100,101,102,103";
            }
            String menusStr = getMenusStr(menuIds);
            Map<String, Object> map = JsonUtil.parseJSON2Map(menusStr);
            JSONArray jsonArray = JSONArray.fromObject(JsonUtil.parseJSON2Map(
                    map.get("result").toString()).get("content"));
            List<Object> objList = JsonUtil.jsonToList(jsonArray.toString());
            List<MenuInfo> menuInfoList = new ArrayList<MenuInfo>();
            for (Object obj : objList) {
                MenuInfo userInfo = JsonUtil.jsonToBean(obj.toString(), MenuInfo.class);
                menuInfoList.add(userInfo);
            }
            for (MenuInfo tree : menuInfoList) {
                if (tree.getParentid() == 0) {
                    rootTrees.add(tree);
                }
                for (MenuInfo t : menuInfoList) {
                    if (t.getParentid() == tree.getId()) {
                        if (tree.getChildList() == null) {
                            List<MenuInfo> myChildrens = new ArrayList<MenuInfo>();
                            myChildrens.add(t);
                            tree.setChildList(myChildrens);
                        } else {
                            tree.getChildList().add(t);
                        }
                    }
                }
            }
        }
        System.out.println(rootTrees);
        // return rootTrees.toString();
        return rootTrees;
    }

    public String getMenusStr(String menuIds) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("target", XMLRead.AIM);
        params.put("option", "select_table");
        params.put("service", "all");
        params.put("object", "table");

        Map<String, Object> dbinfoMap = new HashMap<String, Object>();
        DbInfo dbInfo = XMLRead.xmlDbInfo();
        dbinfoMap.put("type", dbInfo.getType());
        dbinfoMap.put("ip", dbInfo.getIp());
        dbinfoMap.put("port", dbInfo.getPort());
        dbinfoMap.put("dbname", dbInfo.getDbname());
        dbinfoMap.put("user", dbInfo.getUser());
        dbinfoMap.put("passwd", dbInfo.getPassword());
        Map<String, Object> datebaseinfo = new HashMap<String, Object>();
        datebaseinfo.put("dbinfo", dbinfoMap);

        params.put("datebaseinfo", datebaseinfo);

        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell = new HashMap<String, Object>();
        paramsCell.put("name", "menuinfo");
        paramsCell.put("asname", "");
        mapList.add(paramsCell);
        Map<String, Object> tableInfoMap = new HashMap<String, Object>();
        tableInfoMap.put("tables", mapList);
        tableInfoMap.put("from", 0);
        tableInfoMap.put("count", 100);

        List<Map<String, Object>> mapList2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> paramsCell2 = new HashMap<String, Object>();
        paramsCell2.put("name", "*");
        paramsCell2.put("asname", "");
        mapList2.add(paramsCell2);
        tableInfoMap.put("cols", mapList2);

        List<Map<String, Object>> mapList3 = new ArrayList<Map<String, Object>>();

        Map<String, Object> paramsObj3 = new HashMap<String, Object>();
        paramsObj3.put("id", menuIds);
        Map<String, Object> paramsObj4 = new HashMap<String, Object>();
        paramsObj4.put("in", paramsObj3);
        mapList3.add(paramsObj4);

        Map<String, Object> paramsObj5 = new HashMap<String, Object>();
        paramsObj5.put("must", mapList3);
        Map<String, Object> paramsObj6 = new HashMap<String, Object>();
        paramsObj6.put("bool", paramsObj5);

        List<Map<String, Object>> mapList4 = new ArrayList<Map<String, Object>>();
        mapList4.add(paramsObj6);
        Map<String, Object> paramsObj7 = new HashMap<String, Object>();
        paramsObj7.put("should", mapList4);
        Map<String, Object> paramsObj8 = new HashMap<String, Object>();
        paramsObj8.put("bool", paramsObj7);
        tableInfoMap.put("where", paramsObj8);

        params.put("tableinfo", tableInfoMap);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }
}
